import {
  _decorator,
  Component,
  Node,
  NodeEventType,
  resources,
  Sprite,
  SpriteFrame,
  tween,
  UITransform,
  v2,
  v3
} from "cc"
import { AudioMgr } from "./AudioMgr"
const { ccclass, property } = _decorator

@ccclass("GameRoot")
export class GameRoot extends Component {
  cardList: SpriteFrame[] = []
  sorts = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]
  saveCard = {
    saveNode: null,
    saveId: -1
  }
  async start() {
    const index = Math.floor(Math.random() * 3 + 1)
    AudioMgr.inst.play(`bgm/relaxBGM${index}`, 0.5)
    this.cardList = await this.init("card")
    this.sortCards(this.sorts)
    this.createAllCards()
    this.touchCard()
    this.setAllCardBack()
  }
  async init(url: string) {
    return new Promise(resolve => {
      resources.loadDir(url, SpriteFrame, (err, assets) => {
        resolve(assets)
      })
    })
  }
  createAllCards() {
    this.sorts.forEach((item, index) => {
      const node = new Node()
      this.node.addChild(node)
      const uiTrans = node.addComponent(UITransform)
      uiTrans.setContentSize(120, 180)
      const sprite = node.addComponent(Sprite)
      sprite.sizeMode = Sprite.SizeMode.CUSTOM
      sprite.spriteFrame = this.cardList[item]
    })
  }
  touchCard() {
    this.node.children.forEach((node, index) => {
      const id = this.sorts[index]
      node.on(
        Node.EventType.TOUCH_START,
        async () => {
          const { saveNode, saveId } = this.saveCard
          if (node != saveNode) this.turnCard(node, id)
          if (!saveNode) {
            this.setSaveCard(node, id)
          } else {
            this.resetSaveCard()
            if (saveId == id) {
              node.off(Node.EventType.TOUCH_START)
              saveNode.off(Node.EventType.TOUCH_START)
            } else {
              this.setCardBack(node)
              this.setCardBack(saveNode)
            }
          }
        },
        this
      )
    })
  }
  setSaveCard(node, id) {
    this.saveCard = {
      saveNode: node,
      saveId: id
    }
  }
  resetSaveCard() {
    this.saveCard = {
      saveNode: null,
      saveId: -1
    }
  }
  setCardBack(node) {
    tween(node)
      .delay(1)
      .to(0.3, { scale: v3(0, 1, 0) })
      .call(() => {
        const sp = node.getComponent(Sprite)
        sp.spriteFrame = this.getBackCard()
      })
      .to(0.3, { scale: v3(1, 1, 1) })
      .start()
  }
  turnCard(node, id) {
    tween(node)
      .to(0.3, { scale: v3(0, 1, 1) })
      .call(() => {
        const sp = node.getComponent(Sprite)
        sp.spriteFrame = this.getCardById(id)
      })
      .to(0.3, { scale: v3(1, 1, 1) })
      .start()
  }
  setAllCardBack() {
    this.node.children.forEach((node, index) => {
      tween(node)
        .delay(0.3)
        .to(0.3, { scale: v3(0, 1, 0) })
        .call(() => {
          const sp = node.getComponent(Sprite)
          sp.spriteFrame = this.getBackCard()
        })
        .to(0.3, { scale: v3(1, 1, 1) })
        .start()
    })
  }
  getCardById(id) {
    return this.cardList[id]
  }
  getBackCard() {
    return this.cardList[0]
  }
  sortCards(cards: number[]) {
    console.log("排序")
    cards.sort(_ => 0.5 - Math.random())
    cards.sort(_ => 0.5 - Math.random())
    cards.sort(_ => 0.5 - Math.random())
    return cards
  }
}
